# -*- coding: utf-8 -*-
import scrapy
from vin_spider.items import VinSpiderItem
from scrapy.selector import Selector

class CarNoticeSpider(scrapy.Spider):
    name = 'car_notice'
    allowed_domains = ['http://www.cn357.com']
    #start_urls = ['http://www.bssoz.com/html/notice_list/']
    def start_requests(self):
        url = 'http://www.cn357.com/cvi.php?m=cvNotice&search=n&type=%BD%CE%B3%B5&page={0}'
        yield scrapy.Request('http://www.cn357.com/cvi.php?m=cvNotice&search=n&type=%BD%CE%B3%B5', callback=self.middle_parse)
        for i in range(2, 2050):
            yield scrapy.Request(url.format(i), callback=self.middle_parse, dont_filter=True)



    def middle_parse(self,response):
        url = 'http://www.cn357.com'

        b = response.xpath('//ul[@class="searchList uiLinkList"]/li/h3/a/@href')



        for i in b:
            items = VinSpiderItem()
            print(url+i.extract())

            items['url'] = url+i.extract()
            yield scrapy.Request(url+i.extract(), meta={'items': items},callback=self.detailParse, dont_filter=True)




    def detailParse(self, response):

        items = response.meta['items']
        if (len(response.xpath('//h1[@class="itemH1"]//text()')) == 1):
            items['title'] = response.xpath('//h1[@class="itemH1"]//text()')[0].extract()
        else:
            items['title'] = None
        if(len(response.xpath('//td[contains(text(),"变更(扩展)记录")]/../td[2]//a//text()')) != 0):
            items['change_detail'] = ','.join(i.extract() for i in response.xpath('//td[contains(text(),"变更(扩展)记录")]/../td[2]//a//text()'))

        else:
            items['change_detail'] = None
        if(len(response.xpath('//td[contains(text(),"公告型号")]/../td[2]//text()')) > 0):
            items['notice_type'] = response.xpath('//td[contains(text(),"公告型号")]/../td[2]//text()')[0].extract()
        else:
            items['notice_type'] =None
        if(len(response.xpath('//td[contains(text(),"公告批次")]/../td[4]//text()')) > 0):
            items['notice_number'] = response.xpath('//td[contains(text(),"公告批次")]/../td[4]//text()')[0].extract()
        else:
            items['notice_number'] =None
        if (len(response.xpath('//td[contains(text(),"品牌")]/../td[2]//text()')) > 0):
            items['brand'] = response.xpath('//td[contains(text(),"品牌")]/../td[2]//text()')[0].extract()
        else:
            items['brand'] =None
        if(len(response.xpath('//td[contains(text(),"类型")]/../td[4]//text()'))> 0):
            items['car_type'] = response.xpath('//td[contains(text(),"类型")]/../td[4]//text()')[0].extract()
        else:
            items['car_type'] =None
        if (len(response.xpath('//td[contains(text(),"额定质量")]/../td[2]//text()')) > 0):
            items['Rated_quality'] = response.xpath('//td[contains(text(),"额定质量")]/../td[2]//text()')[0].extract()
        else:
            items['Rated_quality'] =None
        if(len(response.xpath('//td[contains(text(),"总质量")]/../td[4]//text()')) > 0):
            items['all_quality'] = response.xpath('//td[contains(text(),"总质量")]/../td[4]//text()')[0].extract()
        else:
            items['all_quality'] =None
        if (len(response.xpath('//td[contains(text(),"整备质量")]/../td[2]//text()')) > 0):
            items['curb_weight'] = response.xpath('//td[contains(text(),"整备质量")]/../td[2]//text()')[0].extract()
        else:
            items['curb_weight'] =None
        if(len(response.xpath('//td[contains(text(),"燃料种类")]/../td[4]/text()')) > 0):
            items['fuel_type'] = response.xpath('//td[contains(text(),"燃料种类")]/../td[4]//text()')[0].extract()
        else:
            items['fuel_type'] =None
        if (len(response.xpath('//td[contains(text(),"排放依据标准")]/../td[2]/text()')) > 0):
            items['Emission_standards'] = response.xpath('//td[contains(text(),"排放依据标准")]/../td[2]//text()')[0].extract()
        else:
            items['Emission_standards'] =None
        if(len(response.xpath('//td[contains(text(),"轴数")]/../td[4]//text()')) > 0):
            items['Axis_number'] = response.xpath('//td[contains(text(),"轴数")]/../td[4]//text()')[0].extract()
        else:
            items['Axis_number'] =None
        if (len(response.xpath('//td[contains(text(),"轴距")]/../td[2]//text()')) > 0):
            items['Wheelbase'] = response.xpath('//td[contains(text(),"轴距")]/../td[2]//text()')[0].extract()
        else:
            items['Wheelbase'] =None
        if(len(response.xpath('//td[contains(text(),"轴荷")]/../td[4]//text()')) > 0):
            items['Axle_load'] = response.xpath('//td[contains(text(),"轴荷")]/../td[4]//text()')[0].extract()
        else:
            items['Axle_load'] =None
        if (len(response.xpath('//td[contains(text(),"弹簧片数")]/../td[2]//text()')) > 0):
            items['Spring_number'] = response.xpath('//td[contains(text(),"弹簧片数")]/../td[2]//text()')[0].extract()
        else:
            items['Spring_number'] =None
        if(len(response.xpath('//td[contains(text(),"轮胎数")]/../td[4]//text()')) > 0):
            items['tires_Number'] = response.xpath('//td[contains(text(),"轮胎数")]/../td[4]//text()')[0].extract()
        else:
            items['tires_Number'] =None
        if (len(response.xpath('//td[contains(text(),"轮胎规格")]/../td[2]//text()')) > 0):
            items['Tire_specification'] = response.xpath('//td[contains(text(),"轮胎规格")]/../td[2]//text()')[0].extract()
        else:
            items['Tire_specification'] =None
        if(len(response.xpath('//td[contains(text(),"接近离去角")]/../td[4]//text()')) > 0):
            items['Close_departure_angle'] = response.xpath('//td[contains(text(),"接近离去角")]/../td[4]//text()')[0].extract()

        else:
            items['Close_departure_angle'] =None
        if (len(response.xpath('//td[contains(text(),"前悬后悬")]/../td[2]//text()')) > 0):
            items['Front_suspension'] = response.xpath('//td[contains(text(),"前悬后悬")]/../td[2]//text()')[0].extract()
        else:
            items['Front_suspension'] =None
        if(len(response.xpath('//td[contains(text(),"前轮距")]/../td[4]//text()')) > 0):
            items['Front_wheelbase'] = response.xpath('//td[contains(text(),"前轮距")]/../td[4]//text()')[0].extract()
        else:
            items['Front_wheelbase'] =None
        if (len(response.xpath('//td[contains(text(),"后轮距")]/../td[2]//text()')) > 0):
            items['Rear_wheelbase'] = response.xpath('//td[contains(text(),"后轮距")]/../td[2]//text()')[0].extract()
        else:
            items['Rear_wheelbase'] =None
        if(len(response.xpath('//td[contains(text(),"识别代号")]/../td[4]//text()')) > 0):
            items['vin'] = response.xpath('//td[contains(text(),"识别代号")]/../td[4]//text()')[0].extract()
        else:
            items['vin'] =None
        if (len(response.xpath('//td[contains(text(),"整车长")]/../td[2]/text()')) > 0):
            items['car_length'] = response.xpath('//td[contains(text(),"整车长")]/../td[2]/text()')[0].extract()
        else:
            items['car_length'] =None
        if(len(response.xpath('//td[contains(text(),"整车宽")]/../td[4]//text()')) > 0):
            items['car_width'] = response.xpath('//td[contains(text(),"整车宽")]/../td[4]//text()')[0].extract()
        else:
            items['car_width'] =None
        if (len(response.xpath('//td[contains(text(),"整车高")]/../td[2]//text()')) > 0):
            items['car_height'] = response.xpath('//td[contains(text(),"整车高")]/../td[2]//text()')[0].extract()
        else:
            items['car_height'] =None
        if(len(response.xpath('//td[contains(text(),"货厢长")]/../td[4]//text()')) > 0):
            items['Cargo_length'] = response.xpath('//td[contains(text(),"货厢长")]/../td[4]//text()')[0].extract()
        else:
            items['Cargo_length'] =None
        if (len(response.xpath('//td[contains(text(),"货厢宽")]/../td[2]//text()')) > 0):
            items['Cargo_width'] = response.xpath('//td[contains(text(),"货厢宽")]/../td[2]//text()')[0].extract()
        else:
            items['Cargo_width'] =None
        if(len(response.xpath('//td[contains(text(),"货厢高")]/../td[4]//text()')) > 0):
            items['Cargo_height'] = response.xpath('//td[contains(text(),"货厢高")]/../td[4]//text()')[0].extract()
        else:
            items['Cargo_height'] =None
        if (len(response.xpath('//td[contains(text(),"最高车速")]/../td[2]//text()')) > 0):
            items['Maximum_speed'] = response.xpath('//td[contains(text(),"最高车速")]/../td[2]//text()')[0].extract()
        else:
            items['Maximum_speed'] =None
        if(len(response.xpath('//td[contains(text(),"额定载客")]/../td[4]//text()')) > 0):
            items['Rated_passenger'] = response.xpath('//td[contains(text(),"额定载客")]/../td[4]//text()')[0].extract()
        else:
            items['Rated_passenger'] =None
        if (len(response.xpath('//td[contains(text(),"驾驶室准乘人数")]/../td[2]//text()')) > 0):
            items['Number_passengers_cab'] = response.xpath('//td[contains(text(),"驾驶室准乘人数")]/../td[2]//text()')[0].extract()
        else:
            items['Number_passengers_cab'] =None
        if(len(response.xpath('//td[contains(text(),"转向形式")]/../td[4]//text()')) > 0):
            items['Steering_Type'] = response.xpath('//td[contains(text(),"转向形式")]/../td[4]//text()')[0].extract()
        else:
            items['Steering_Type'] =None
        if (len(response.xpath('//td[contains(text(),"准拖挂车总质量")]/../td[2]//text()')) > 0):
            items['Quasi_trailer_total_quality'] = response.xpath('//td[contains(text(),"准拖挂车总质量")]/../td[2]//text()')[0].extract()
        else:
            items['Quasi_trailer_total_quality'] =None
        if(len(response.xpath('//td[contains(text(),"载质量利用系数")]/../td[4]//text()')) > 0):
            items['Load_mass_utilization_factor'] = response.xpath('//td[contains(text(),"载质量利用系数")]/../td[4]//text()')[0].extract()
        else:
            items['Load_mass_utilization_factor'] =None
        if (len(response.xpath('//td[contains(text(),"半挂车鞍座最大承载质量")]/../td[2]//text()')) > 0):
            items['Maximum_load_bearing_quality_saddle'] = response.xpath('//td[contains(text(),"半挂车鞍座最大承载质量")]/../td[2]//text()')[0].extract()
        else:
            items['Maximum_load_bearing_quality_saddle'] =None

        if(len(response.xpath('//td[contains(text(),"企业名称")]/../td[4]//text()')) > 0):
            items['car_factory'] = response.xpath('//td[contains(text(),"企业名称")]/../td[4]//text()')[0].extract()
        else:
            items['car_factory'] =None
        if (len(response.xpath('//td[contains(text(),"企业地址")]/../td[2]//text()')) > 0):
            items['address'] = response.xpath('//td[contains(text(),"企业地址")]/../td[2]//text()')[0].extract()
        else:
            items['address'] =None
        if(len(response.xpath('//td[contains(text(),"电话号码")]/../td[4]//text()')) > 0):
            items['telephone_number'] = response.xpath('//td[contains(text(),"电话号码")]/../td[4]//text()')[0].extract()
        else:
            items['telephone_number'] =None
        if (len(response.xpath('//td[contains(text(),"传真号码")]/../td[2]//text()')) > 0):
            items['fax'] = response.xpath('//td[contains(text(),"传真号码")]/../td[2]//text()')[0].extract()
        else:
            items['fax'] =None
        if(len(response.xpath('//td[contains(text(),"邮政编码")]/../td[4]//text()')) > 0):
            items['zip_code'] = response.xpath('//td[contains(text(),"邮政编码")]/../td[4]//text()')[0].extract()

        else:
            items['zip_code'] =None
        if (len(response.xpath('//td[contains(text(),"底盘1")]/../td[2]//text()')) > 0):
            items['Chassis_1'] = response.xpath('//td[contains(text(),"底盘1")]/../td[2]//text()')[0].extract()
        else:
            items['Chassis_1'] =None
        if(len(response.xpath('//td[contains(text(),"底盘2")]/../td[4]//text()')) > 0):
            items['Chassis_2'] = response.xpath('//td[contains(text(),"底盘2")]/../td[4]//text()')[0].extract()
        else:
            items['Chassis_2'] =None
        if (len(response.xpath('//td[contains(text(),"底盘3")]/../td[2]//text()')) > 0):
            items['Chassis_3'] = response.xpath('//td[contains(text(),"底盘3")]/../td[2]//text()')[0].extract()
        else:
            items['Chassis_3'] =None
        if(len(response.xpath('//td[contains(text(),"底盘4")]/../td[4]//text()')) > 0):
            items['Chassis_4'] = response.xpath('//td[contains(text(),"底盘4")]/../td[4]//text()')[0].extract()
        else:
            items['Chassis_4'] =None
        if (len(response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[1]//text()')) > 0):
            items['engine_model'] = response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[1]//text()')[0].extract()
        else:
            items['engine_model'] =None
        if(len(response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[2]//text()')) > 0):
            items['Engine_manufacturer'] = response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[2]//text()')[0].extract()
        else:
            items['Engine_manufacturer'] =None
        if (len(response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[4]//text()')) > 0):
            items['Displacement'] = response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[4]//text()')[0].extract()
        else:
            items['Displacement'] =None
        if(len(response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[5]//text()')) > 0):
            items['power'] = response.xpath('//td[contains(text(),"发动机型号")]/../../tr[2]/td[5]//text()')[0].extract()
        else:
            items['power'] =None
        if (len(response.xpath('//td[contains(text(),"备注")]/../td[2]//text()')) >0):
            items['note'] = ','.join([i.extract() for i in response.xpath('//td[contains(text(),"备注")]/../td[2]//text()')]).strip()

        else:
            items['note'] =None



        yield items



